/**
 * 
 */
package com.sunwayhorizo.health.common;

import java.lang.reflect.Field;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

/**
 * @ClassName StrUtil <br>
 * @PackageName com.sunwayhorizo.health.common <br>
 * @Description 字符串验证工具，用来验证字符串是否为空， 是否为电子邮件 ，是否为电话等<br>
 * @Author jinzw <br>
 * @Date 2010-12-10 <br>
 * @Version 1.0 <br>
 * @ModifiedDesc 增加方法<br>
 * @ModifiedDate 2011-03-17 <br>
 * @ModifiedBy obama<br>
 * @Email tanll@sunwaysoft.com.cn <br>
 * @Copyright 2010-2020 北京三维海容 All Rights Reserved. <br>
 */
public class StrUtil {

	/**
	 * 用来验证字符串是否为空（"" ,"   "）或者null 如果字符串里面内容全是空格或者""或者null，返回true，否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isBlank(String str) {
		return StringUtils.isBlank(str);
	}

	public static boolean isNotBlank(String str) {
		return !StringUtils.isBlank(str);
	}

	/**
	 * 用来验证字符串是否为空（""）或者null 如果字符串里面内容是""或者null，返回true，否则返回false
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isEmpty(String str) {
		return StringUtils.isEmpty(str);
	}

	public static boolean isNotEmpty(String str) {
		return !StringUtils.isEmpty(str);
	}

	/**
	 * 验证字符串是否为电子邮件 如果是电子邮件则返回true，不是则返回false
	 * 
	 * @param email
	 * @return
	 */
	public static boolean isEmail(String email) {
		if (StringUtils.isEmpty(email))
			return false;
		String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(email);
		matcher.matches();
		return matcher.matches();
	}

	/**
	 * 验证字符串是否为手机号 如果是手机号则返回true，不是则返回true
	 * 
	 * @param phone
	 * @return
	 */
	public static boolean isCellPhone(String phone) {
		if (StringUtils.isEmpty(phone))
			return false;
		String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(phone);
		return matcher.matches();
	}

	/**
	 * 判断字符串是否为ip 如果是ip则返回true，不是则返回false
	 * 
	 * @param ip
	 * @return
	 */
	public static boolean isIp(String ip) {
		if (StringUtils.isEmpty(ip))
			return false;
		String check = "^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
		Pattern regex = Pattern.compile(check);
		Matcher matcher = regex.matcher(ip);
		return matcher.matches();
	}

	/**
	 * 一下参数调用完这个方法后的结果为： "a-b-c"
	 * @param as  ["a","b","c"]
	 * @param separator "-"
	 * @return
	 */
	public static String join(String[] as, String separator) {
		if (as == null)
			return null;
		else if (as.length == 1)
			return as[0];
		else {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < as.length; ++i) {
				if (i > 0)
					sb.append(separator);
				sb.append(as[i]);
			}
			return sb.toString();
		}
	}

	/**
	 * 格式化字符串 作者 jinzw <br>
	 * 创建日期 2010-12-13 <br>
	 * 
	 * @param str
	 *            需要格式化的字符串
	 * @return String
	 */
	public static String formatStr(String str) {
		if(StrUtil.isEmpty(str)) return null;
		String newStr = "";
		if (StrUtil.isNotEmpty(str)) {
			if (str.contains("-")) {
				String[] strs = str.split("-");
				for (int i = 0; i < strs.length; i++) {
					newStr += strs[i];
				}
				return newStr;
			} else if (str.contains("/")) {
				String[] strs = str.split("/");
				for (int i = 0; i < strs.length; i++) {
					newStr += strs[i];
				}
				return newStr;
			} else
				return str;
		}
		return newStr;
	}

	/**
	 * 把字符串格式化成日期形式的字符串
	 * 
	 * @param str
	 * @return
	 */
	public static String formatNewStr(String str) {
		String year = str.substring(0, 4);
		String month = str.substring(4, 6);
		String day = str.substring(6, 8);
		String dataStr = year + "/" + month + "/" + day;
		return dataStr;
	}

	/**
	 * 根据出生日期计算年龄 作者 jinzw <br>
	 * 创建日期 2010-12-25 <br>
	 * 
	 * @param birthday
	 *            出身日期
	 * @return int
	 */
	public static int calcAge(String birthday) {
		birthday = formatStr(birthday);
		int iage = 0;
		if (birthday != "" || birthday != null) {
			int year = Integer.parseInt(birthday.substring(0, 4));
			int month = Integer.parseInt(birthday.substring(4, 6));
			int day = Integer.parseInt(birthday.substring(6, 8));

			Calendar birthDate = new GregorianCalendar(year, month, day);
			Calendar today = Calendar.getInstance();

			if (today.get(Calendar.YEAR) > birthDate.get(Calendar.YEAR)) {
				iage = today.get(Calendar.YEAR) - birthDate.get(Calendar.YEAR)
						- 1;
				if (today.get(Calendar.MONTH) + 1 > birthDate
						.get(Calendar.MONTH)) {
					iage++;
				} else if (today.get(Calendar.MONTH) + 1 == birthDate
						.get(Calendar.MONTH)) {
					if (today.get(Calendar.DAY_OF_MONTH) > birthDate
							.get(Calendar.DAY_OF_MONTH)) {
						iage++;
					}
				}
			}
			return iage;
		}
		return 0;
	}
	
	/**
	 * String null to "" 作者obama <br>
	 * 创建日期 2010-12-25 <br>
	 * 
	 * @param str
	 *           
	 * @return String
	 */
	public static String removeNull(String cotainNull){
		if(cotainNull==null){
			return "";
		}else{
			return cotainNull;
		}
	}
	
	   /**
     * 判断数组中是否所有的字符串是否都为空
     * 
     * @param 给定的字符串或字符串数组
     * @return 数组中字符串全为空时，返回结果为true
     */
    public static boolean isAllEmpty(String... strings) {
        boolean nullFlag = true;
        if (strings == null) {
            return nullFlag;
        }

        for (String string : strings) {
            if (!isEmpty(string)) {
                nullFlag = false;
                break;
            }
        }
        return nullFlag;
    }

    /**
     * 判断数组中是否有值  
     * 
     * @param 给定的字符串或字符串数组
     * @return 数组中字符串若有一个不为空，返回结果为true
     */
    public static boolean isNotAllEmpty(String... strings) {
        boolean hasFlag = false;
        if (strings == null) {
            return hasFlag;
        }

        for (String string : strings) {
            if (isNotEmpty(string)) {
                hasFlag = true;
                break;
            }
        }
        return hasFlag;
    }
    
    /**
     * 判断数组中是否所有的字符串都不为空
     * 
     * @param 给定的字符串或字符串数组
     * @return 数组中字符串全有值时，返回结果为true
     */
    public static boolean isAllFilled(String... strings) {
        if (strings == null ) {
            return false;
        }

        for (String string : strings) {
            if (isEmpty(string)) {
                return false;
            }
        }
        return true;
    }
    
    /**
     * 判断数组中是否所有的字符串都不为空
     * 
     * @param 给定的字符串或字符串数组
     * @return 数组中字符串全有值时，返回结果为true
     */
    public static boolean isMinusOne(String number) {
        if( StrUtil.isNotEmpty(number) )
        {
            if( "-1".equals(number)){
                return true;
            }
            else{
                return false;
            }
            
        }
        else
        {
            return false;
        }
    }
}
